Date.prototype.stdTimezoneOffset = function() {
	var jan = new Date(this.getFullYear(), 0, 1);
	var jul = new Date(this.getFullYear(), 6, 1);
	return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.dst = function() {
	return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
Date.prototype.getTimezone = function() {
	var tz = this.getTimezoneOffset() / 60;
	return (this.dst) ? tz : tz;
}

String.prototype.nl2br = function() {
	return (this + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br />$2');
}
function nl2br(str) {   
	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br />$2');
}

/**
 * facebook UI Object
 * 
 */
var fbUI = {
	message: {
		unread: 0, 
		all: 0
	},
	scope: {
		scope:'email,user_location'
	},
	//facebook login
	login: function(url) {
		FB.login(function(response) {
			//_push({"key":"fblogin", "val":response});
			if (response.authResponse) {
				FB.api('/me?fields=id,name,location,gender,picture,email', function(resp) {
					$.post(url, resp, function(data) {
						var json = wpu.parseJSON(data);
						if (json.head == 'E') {
							alert(json.data);
						} else location.href = json.data.href;
					});
				});
			}
		}, this.scope);
	},
	//invite friends: apprequests
	invite: function(title, message) {
		FB.login(function(response) {
			if (response.authResponse) {
				// user successfully logged in 
				FB.ui({
					method:'apprequests', 
					link:'http://www.yawii.com',
					title:title, 
					message:message
				}, function (response) {
					_push({
						"key":"fbinvite", 
						"val":response
					});
				});
			}
		}, this.scope);
	},
	//recommend url: method=feed, send
	recommend: function(url, method, callback) {
		FB.login(function(response) {
			if (response.authResponse) {
				FB.ui({
					method:method, 
					link:url
				}, function (response) {
					callback(response);
				});
			}
		}, this.scope);
	},
	share: function(url) {
		window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url), 'Share', 'toolbar=0, menubar=0, width=640, height=320');
	}
};

var wForm = {
	validateResult: function(json) {
		if (json.success) {
			alert(json.success);
		} else {
			for (var name in json) {
				$('#'+name).focus();
				return;
			}
		}
	}
};

// loading if document ready
$(function() {
	handle_side_menu();
	widget_boxes();
	
	//set countdown
	$('[rel="tipsy-bottom"]').tooltip({placement:'bottom'});
	$('[rel="tipsy-top"]').tooltip({placement:'top'});
});

function handle_side_menu() {
	$('#menu-toggler').on('click', function() {
		$('#sidebar').toggleClass('display');
		$(this).toggleClass('display');
		return false;
	});
	//mini
	var $minimized = false;
	$('#sidebar-collapse').on('click', function(){
		$('#sidebar').toggleClass('menu-min');
		$(this.firstChild).toggleClass('icon-double-angle-right');
		
		$minimized = $('#sidebar').hasClass('menu-min');
		if($minimized) {
			$('.open > .submenu').removeClass('open');
		}
	});
	//opening submenu
	$('.nav-list').on('click', function(ev) {
		var link_element = $(ev.target).closest('a');
		if (!link_element || link_element.length == 0) return true;//if not clicked inside a link element
		if ($minimized) {
			var text = link_element.find('.menu-text').get(0);
			if (ev.target != text && !$.contains(text, ev.target) )//not clicking on the text or its children
				return false;
		}
		//check to see if we have clicked on an element which is inside a .dropdown-toggle element?!
		//if so, it means we should toggle a submenu
		var link_element = $(ev.target).closest('.dropdown-toggle');
		if(link_element && link_element.length > 0) {
			var sub = link_element.next().get(0);
			//if we are opening this submenu, close all other submenus except the ".active" one
			if(! $(sub).is(':visible') ) {//ie, we are about to open it and make it visible
				$('.open > .submenu').each(function(){
					if(this != sub && !$(this.parentNode).hasClass('active')) {
						$(this).slideUp(200).parent().removeClass('open');
					}
				});
			}
			$(sub).slideToggle(200).parent().toggleClass('open');
			return false;
		}
		return true;
	});
}

function widget_boxes() {
	$('.widget-toolbar > a[data-action]').each(function() {
		var $this = $(this);
		var $action = $this.data('action');
		var $box = $this.closest('.widget-box');
		
		if($action == 'collapse') {
			var $body = $box.find('.widget-body');
			var $icon = $this.find('[class*=icon-]').eq(0);
			var $match = $icon.attr('class').match(/icon\-(.*)\-(up|down)/);
			var $icon_down = 'icon-'+$match[1]+'-down';
			var $icon_up = 'icon-'+$match[1]+'-up';
			
			$body = $body.wrapInner('<div class="widget-body-inner"></div>').find(':first-child').eq(0);
			$this.on('click', function(ev){
				if($box.hasClass('collapsed')) {
					if($icon) $icon.addClass($icon_up).removeClass($icon_down);
					$box.removeClass('collapsed');
					$body.slideDown(200);
				}
				else {
					if($icon) $icon.addClass($icon_down).removeClass($icon_up);
					$body.slideUp(300, function(){$box.addClass('collapsed')});
				}
				ev.preventDefault();
			});
			if($box.hasClass('collapsed') && $icon) $icon.addClass($icon_down).removeClass($icon_up);
		} else if($action == 'close') {
			$this.on('click', function(ev){
				$box.hide(300 , function(){$box.remove();});
				ev.preventDefault();
			});
		} else if($action == 'reload') {
			$this.on('click', function(ev){
				$this.blur();
				var $remove = false;
				if(!$box.hasClass('position-relative')) {$remove = true; $box.addClass('position-relative');}
				$box.append('<div class="widget-box-layer"><i class="icon-spinner icon-spin icon-2x white"></i></div>');
				setTimeout(function(){
					$box.find('> div:last-child').remove();
					if($remove) $box.removeClass('position-relative');
				}, parseInt(Math.random() * 1000 + 1000));
				ev.preventDefault();
			});
		} else if($action == 'settings') {
			$this.on('click', function(ev){
				ev.preventDefault();
			});
		}
	});
}